import java.util.TreeSet;


public class myTester5 {
	public static void main(String[] args) {
		for(int i = 0; i < 2000; i++) {
			if (!tester() ) return;
		}
		System.out.println("finished 200 checks");
	}
	
	public static boolean tester() {
		System.out.println("\n\n\n\n");
		Heap myHeap = new Heap();
		double chance = Math.random();
		int numItems = (int)(50 * chance);
		TreeSet<Integer> valueTree = new TreeSet<Integer>();
		
		for(int i = 0; i < numItems; i++){
			double rand = Math.random();
			int insertVal = (int)(10000 * rand);
			System.out.println("inserting " + insertVal);
			if(valueTree.contains(insertVal) == false) {
				//insert value only if it's diff' from all the others inside
				myHeap.insert(insertVal);
				valueTree.add(insertVal);
			}
			else
			{
				--i;
			}
			if(myHeap.isHeap() == false) {
				System.out.println("is heap says no");
				System.out.println(myHeap);
				return false;
			}
			if(myHeap.contains(insertVal) == false) {
				System.out.println("problem with contains");
				System.out.println("should contain: " + insertVal);
				System.out.println(myHeap);
				return false;
			}
		}
		
		double delChance = Math.random();
		int numDels = (int)(numItems * delChance);
		
		if(myHeap.getHeapsize() != valueTree.size()) {
			System.out.println("problem with size");
			System.out.println("heap: " + myHeap.getHeapsize());
			System.out.println("tree: " + valueTree.size());
			return false;
		}
		if(myHeap.getHeapsize() != 0){
			if(myHeap.getMin() != valueTree.first()) {
				System.out.println("diff minimum");
				return false;
			}
		}
		
		for(int i = 0; i < numDels; i++){
			int currMin = -1;
			if(myHeap.getHeapsize() != 0) {
				currMin = valueTree.first();
			}
			System.out.println("deleting " + currMin);
			myHeap.deleteMin();
			valueTree.remove(valueTree.first());
			if(currMin != -1) {
				if(myHeap.contains(currMin)) {
					System.out.println("problem with contains");
					System.out.println("should not contain: " + currMin);
					System.out.println(myHeap);
					return false;
				}
			}
			if(myHeap.isHeap() == false) {
				System.out.println("is heap says no");
				System.out.println(myHeap);
				return false;
			}
		}
		
		if(myHeap.getHeapsize() != valueTree.size()) {
			System.out.println("problem with size");
			System.out.println("heap: " + myHeap.getHeapsize());
			System.out.println("tree: " + valueTree.size());
			return false;
		}
		if(myHeap.getHeapsize() != 0){
			if(myHeap.getMin() != valueTree.first()) {
				System.out.println("diff minimum");
				return false;
			}
		}
		
		return true;
	}

}
